package com.ruoyi.web.controller.userheath.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.controller.userheath.entity.UserHealth;
import com.ruoyi.web.controller.userheath.service.UserHealthService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("/health")
public class UserHealthController {

    @Autowired
    private UserHealthService userHealthService;

    /**
     * 一次性添加或更新步数、体重、卡路里
     */
    @PostMapping("/updateHealthData")
    public AjaxResult updateHealthData(
            @RequestBody Map<String,Object> param
    ) {
        Long userId =  new BigDecimal(param.get("userId").toString()).longValue();
        int steps =  new BigDecimal(param.get("steps").toString()).intValue();
        double weight =  new BigDecimal(param.get("weight").toString()).doubleValue();
        double calories =  new BigDecimal(param.get("calories").toString()).doubleValue();

        // 更新步数
        boolean stepSuccess = userHealthService.recordSteps(userId, steps);
        // 更新体重
        boolean weightSuccess = userHealthService.updateWeight(userId, weight);
        // 更新卡路里
        boolean caloriesSuccess = userHealthService.recordCalories(userId, calories);

        // 返回结果
        if (stepSuccess && weightSuccess && caloriesSuccess) {
//            return "健康数据更新成功！";
            return AjaxResult.success();
        } else {
//            return "健康数据更新失败！";
            return AjaxResult.error();
        }
    }

    /**
     * 获取某个日期的健康数据
     */
    @PostMapping("/getByDate")
    public AjaxResult getHealthDataByDate(@RequestBody Map<String, Object> map) {
        String userid = (String) map.get("userId");
        String date = (String) map.get("date");
        long userId = Long.parseLong(userid);


        UserHealth healthDataByDate = userHealthService.getHealthDataByDate(userId, LocalDate.parse(date));
        if (healthDataByDate == null) {
            return AjaxResult.error("暂未数据！");
        }
        return AjaxResult.success(healthDataByDate);
    }

    /**
     * 获取所有健康数据
     */
    @GetMapping("/list")
    public AjaxResult getAllHealthData(@RequestParam Long userId) {
        return AjaxResult.success(userHealthService.getAllHealthData(userId));
    }

    /**
     * 获取健康数据的echarts图表
     * @author WanShen
     * @date 2024/12/14 下午10:37
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/getEchartsData")
    public AjaxResult getEchartsData() {
        Echarts echarts = new Echarts();
        List<Echarts> result = new ArrayList<>();

        Long userId = SecurityUtils.getUserId();
        List<UserHealth> allHealthData = userHealthService.getAllHealthData(userId);
        Integer stepsCount = 0;
        Double caloriesBurnedCount = 0.00;
        for (UserHealth allHealthDatum : allHealthData) {
            //总步数
            stepsCount += allHealthDatum.getSteps();
            //总消耗KCAL
            caloriesBurnedCount += allHealthDatum.getCaloriesBurned();
        }
        echarts.setValue(stepsCount);
        echarts.setName("总步数");
        result.add(echarts);

        echarts = new Echarts();
        echarts.setValue(caloriesBurnedCount);
        echarts.setName("总消耗KCAL");
        result.add(echarts);


        OptionalDouble max = allHealthData.stream().mapToDouble(v -> v.getWeight()).max();
        OptionalDouble min = allHealthData.stream().mapToDouble(v -> v.getWeight()).min();
        if (max.isPresent() && min.isPresent()) {
//            double weightDifference = max.getAsDouble() - min.getAsDouble();
            echarts = new Echarts();
            echarts.setValue(max.getAsDouble());
            echarts.setName("最高体重");
            result.add(echarts);
            echarts = new Echarts();
            echarts.setValue(min.getAsDouble());
            echarts.setName("最低体重");
            result.add(echarts);
        }


        return AjaxResult.success(result);
    }

    /**
     *获取当前登录用户的所有健康计划
     * @author WanShen
     * @date 2025/2/15 上午10:59
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/getAllPlan")
    public AjaxResult getAllPlan() {
        Long userId = SecurityUtils.getUserId();
        List<UserHealth> allHealthData = userHealthService.getAllHealthData(userId);
        Long allPlanCount = userHealthService.getAllPlanCount(userId);
        Map<String, Object> data = new HashMap<>();
        data.put("data", allHealthData);
        data.put("total", allPlanCount);
        return AjaxResult.success(data);
    }

    @Data
    class Echarts {
        private Object value;
        private String name;
    }

}

